<?php


namespace Api\Controller;


class OauthController extends BaseController
{

    private $authResult = array();
    private $type = '';

    public function __construct()
    {
        parent::__construct();
        $aType = I_POST('type', 'text');
        $this->type = $this->getType($aType);

        $aAuthResult = I_POST('auth_result', 'text');
        $aAuthResult = json_decode($aAuthResult,true);
        $this->authResult =  D('Api/Oauth')->getToken($aAuthResult,$this->type);  ;

    }

    public function oauth()
    {

        $aUserInfo = I_POST('user_info', 'text');
        $aBindInfo = I_POST('bind_info', 'text');
        $type = $this->type;

        $aUserInfo = json_decode($aUserInfo,true);

        $aBindInfo = json_decode($aBindInfo,true);
        $user_info = D('Api/Oauth')->$type($aUserInfo);

        if (empty($aBindInfo)) {
            //不绑定帐号

            $map = array('type_uid' => $this->authResult['openid'], 'type' => $type);
            $uid = D('sync_login')->where($map)->getField('uid');

            if ($uid) {
                //同步表中存在数据
                $user = UCenterMember()->where(array('id' => $uid))->count();
                if (empty($user)) {
                    D('sync_login')->where($map)->delete();
                    $uid = $this->addData($user_info);
                }
            } else {
                $uid = $this->addData($user_info);
            }

        } else {
            //绑定帐号

        }

        $this->loginWithoutpwd($uid);
    }


    /**
     * loginWithoutpwd  使用uid直接登陆，不使用帐号密码
     * @param $uid
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function loginWithoutpwd($uid)
    {
        if (0 < $uid) { //UC登陆成功
            /* 登陆用户 */
            if ($return = D('Api/Account')->login($uid)) { //登陆用户

                $this->apiSuccess($return, D('Api/User')->getUserInfo($uid));
            } else {
                $this->apiError('登录失败');
            }
        }
    }

    private function getType($type)
    {
        switch ($type) {
            case 'qq';
                $rs = 'qq';
                break;
            case 'weixin';
                $rs = 'weixin';
                break;
            case 'sinaweibo';
                $rs = 'sina';
                break;
            default:
                $rs = '';
        }
        return $rs;
    }


    /**
     * addSyncLoginData  增加sync_login表中数据
     * @param $uid
     * @return mixed
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function addSyncLoginData($uid)
    {
        $data['uid'] = $uid;
        $data['type_uid'] = $this->authResult['openid'];
        $data['oauth_token'] = $this->authResult['access_token'];
        $data['oauth_token_secret'] = $this->authResult['openid'];
        $data['type'] = $this->type;
        $syncModel = D('sync_login');

        if (!($syncModel->where($data)->count())) {
           $syncModel->add($data);
        }
        return true;
    }


    private function addData($user_info)
    {
        $ucenterModer = UCenterMember();
        $uid = $ucenterModer->addSyncData();
        D('Member')->addSyncData($uid, $user_info);
        $ucenterModer->initRoleUser(1, $uid); //初始化角色用户
        // 记录数据到sync_login表中
        $this->addSyncLoginData($uid);
        $this->saveAvatar($user_info['head'], $uid);
        return $uid;
    }


    /**
     * saveAvatar  保存头像到本地
     * @param $url
     * @param $oid
     * @param $uid
     * @param $type
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function saveAvatar($url, $uid)
    {
        $driver = modC('PICTURE_UPLOAD_DRIVER', 'local', 'config');

        if ($driver == 'local') {
            mkdir('./Uploads/Avatar/' . $uid, 0777, true);
            $img = file_get_contents($url);
            $filename = './Uploads/Avatar/' . $uid . '/crop.jpg';
            file_put_contents($filename, $img);
            $data['path'] = '/' . $uid . '/crop.jpg';
        } else {
            $name = get_addon_class($driver);
            $class = new $name();
            $path = '/Uploads/Avatar/' . $uid . '/crop.jpg';
            $res = $class->uploadRemote($url, 'Uploads/Avatar/' . $uid . '/crop.jpg');
            if ($res !== false) {
                $data['path'] = $res;
            }
        }
        $data['uid'] = $uid;
        $data['create_time'] = time();
        $data['status'] = 1;
        $data['is_temp'] = 0;
        $data['driver'] = $driver;
        D('avatar')->add($data);
    }


    ///-------------------------------------------


}